﻿var changePasswordVM = {
    CurrPass: ko.observable(""),
    CurrPassException: ko.observable(""),
    NewPass: ko.observable(""),
    NewPassException: ko.observable(""),
    ReType: ko.observable(""),
    ReTypeException: ko.observable(""),
    clearOldData: function(){
        //clear old data
        this.CurrPass("");
        this.NewPass("");
        this.ReType("");
        this.clearOldMessage();
    },
    clearOldMessage: function(){
        //clear old message
        this.CurrPassException("");
        this.NewPassException("");
        this.ReTypeException("");
    },
    savePassword: function (indexController) {
        var self = this;
        self.clearOldMessage();
        var change = ko.toJSON(this);
        var changeObject = JSON.parse(change);
        var flag = true;
        if (changeObject.CurrPass.length == 0) {
            self.CurrPassException(messageController.messageData["type_password"].text);
            flag = false;
        }
        if (changeObject.NewPass.length == 0) {
            self.NewPassException(messageController.messageData["type_password"].text);
            flag = false;
        }
        if (changeObject.ReType.length == 0) {
            self.ReTypeException(messageController.messageData["type_password"].text);
            flag = false;
        }
        if (changeObject.NewPass != changeObject.ReType) {
            self.ReTypeException(messageController.messageData["no_matched_newpass"].text);
            flag = false;
        }
        if (flag == false) {
            $("#CurrPass").focus();
            return;
        }
        //unhandling
        indexController.enableBtnChangePasswordSave(false);
        indexController.enableBtnChangePasswordCancel(false);
        messageController.processing("processing");
        $.ajax({
            type: 'post',
            url: home + 'Patient/SavePassword',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            data: ko.toJSON({ CurrPass: changeObject.CurrPass, NewPass: changeObject.NewPass, ReType: changeObject.ReType }),
            //data: ko.toJSON({ CurrPass: self.CurrPass(), NewPass: self.NewPass(), ReType: self.ReType() }),//new data wasn't updated in VM
            success: function (data) {
                for (var i in data) {
                    switch (data[i]) {
                        case "invalid_password":
                            self.CurrPassException(messageController.messageData[data[i]].text);
                            break;
                        case "same_password":
                            self.NewPassException(messageController.messageData[data[i]].text);
                            break;
                        case "save_success":
                            indexController.displayChangePasswordForm(false);
                            messageController.processComplete("save_success");
                            break;
                        case "save_fail":
                            messageController.processComplete("save_fail");
                            break;
                    }
                }
            },
            error: function (err) {
                if (err.status == 200) {
                    //messageController.processComplete(err.responseText);
                }
                else {
                    messageController.processComplete("process_error");
                }
            },
            complete: function (xhr, textStatus) {
                indexController.enableBtnChangePasswordSave(true);
                indexController.enableBtnChangePasswordCancel(true);
            }
        });
    }
};